import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'CountController.dart';
import 'TestController.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 4/10/21.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
///代码清单7-13-1
///lib/page/count/CountTestAPage.dart
class CountTestAPage extends StatefulWidget {
  @override
  _CountTestAPageState createState() => _CountTestAPageState();
}
class _CountTestAPageState extends State<CountTestAPage> {
  //注入GetxController对象
  TestController _controller = Get.put(TestController(),permanent: false);
  @override
  void initState() {
    super.initState();
    //异步请求网络数据
    _controller.requestNetData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("计数器"),
      ),
      backgroundColor: Colors.white,
      ///填充布局
      body: Container(
        width: double.infinity,
        height: double.infinity,
        alignment: Alignment.center,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            BPage(),
            SizedBox(
              height: 40,
            ),
            CPage(),
          ],
        ),
      ),
    );
  }
}
///代码清单7-13-2
class BPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      color: Colors.grey,
      child: GetBuilder<TestController>(
        builder: (TestController controller) {
          return Text("B组件 计数 ${controller.count}");
        },
      ),
    );
  }
}

///代码清单7-13-3
class CPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      decoration: BoxDecoration(border: Border.all()),//边框
      child: Row(//水平排列文本与按钮
        mainAxisSize: MainAxisSize.min,//包裹子Widget
        children: [
          //数据监听
          GetBuilder<TestController>(
            builder: (TestController controller) {
              return Text("CPage 计数 ${controller.count}");
            },
          ),
          SizedBox(width: 10,),//间隔
          ElevatedButton(
            onPressed: () {
              //判断是否已成功依赖注入
              bool isRegister = Get.isRegistered<CountController>();
              if(isRegister) {
                //操作数据
                Get.find<CountController>().add();
              }
            },
            child: Text("修改值"),
          ),
        ],
      ),
    );
  }
}
